package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.service.SmsService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class MobileOrderController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private SetMealService setMealService;

    @PostMapping("/submit")
    public Result submit(@RequestBody OrderSubmitParam param){
        log.info("前后端联通：{}",param);

        if (!smsService.checkVaildateCode("ORDER", param.getTelephone(),param.getValidateCode())) {
            return new Result(false,MessageConst.VALIDATECODE_ERROR);
        }

        Member member = new Member();
        member.setName(param.getName());
        member.setIdCard(param.getIdCard());
        member.setSex(param.getSex());
        member.setPhoneNumber(param.getTelephone());
        member = memberService.creatMember(member);

        Order order = new Order();
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(param.getSetMealId());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderDate(param.getOrderDate());
        order.setMemberId(member.getId());
        order = orderService.add(order);
        return new Result(true, MessageConst.ORDERSETTING_SUCCESS,order);
    }
//    @GetMapping("/findById")
//    public Result findById(Integer id) {
//        log.info("查询数据id:{}", id);
//        Order order = orderService.findById(id);
//        Member member = memberService.findById(order.getMemberId());
//        Setmeal setmeal = setMealService.findById(order.getSetmealId());
//        Map<String, Object> map = new HashMap<>();
//        map.put("member", member.getName());
//        map.put("setmeal", setmeal.getName());
//        map.put("orderDate", new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
//        map.put("orderType", order.getOrderType());
//
//        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, map);
//    }
    @GetMapping("/findById")
    public Result findById(Integer id){
        Map map = orderService.findById(id);
        return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
}

}
